
var imageBucketRules = {

	'.imagePublish': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id && $('infoBumper'+id).style.display == 'none' ) {
				var span = document.createElement( 'span' );
				span.className = 'infoBumper';
				
				new Insertion.Top( span, 'Are you sure you want to publish this image?' );
				
				var button = document.createElement( 'input' );
				button.type = 'button';
				button.value = 'Publish';
				button.className = 'imagePublishNow';
				button.setAttribute( 'for', id );
				button.style.marginLeft = '1em';
				button.style.marginRight = '1em';
				
				var href = document.createElement( 'a' );
				href.href = '#';
				href.innerHTML = 'Cancel';
				href.className = 'imagePublishCancel';
				href.id = 'imagePublishCancel'+id;
				
				span.appendChild( button );
				span.appendChild( href );
				
				$('infoBumper'+id).appendChild( span );

				Behaviour.apply();

				new Effect.BlindDown( 'infoBumper'+id, {duration: .2} );
			}
		}
	},
	
	'.imagePublishCancel': function( element ) {
		element.onclick = function() {
			new Effect.BlindUp( element.parentNode.parentNode, {duration: .2} );
			Element.remove( element.parentNode );
		}
	},
	
	'.imagePublishNow': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			
			if( id ) {
	
				var spinner = document.createElement( 'img' );
				spinner.id = 'articleEditSpinner';
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				$('infoBumper'+id).appendChild( spinner );
				
				var url = BASEURL+'/'+MKEY+'/publish';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id, evalScripts: true, method: 'post'}
				);
				
				window.publish = function( data ) {

					if( data.saved ) {
					
						Element.remove( spinner );
						$('imagePublishCancel'+id).onclick();
					
						$('imagePublishStatus'+id).src =
							$('imagePublishStatus'+id).src.replace(
								new RegExp( 'star_inactive.png' ),
								'star.png'
							);
							
						actionMessage( 'Image '+id+' Published!' );
					}

				}
			}			
			

		}
	},

	'.imageDelete': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id && $('infoBumper'+id).style.display == 'none' ) {
				var span = document.createElement( 'span' );
				span.className = 'infoBumper';
				
				new Insertion.Top( span, 'Are you sure you want to <b>delete</b> this image?' );
				
				var button = document.createElement( 'input' );
				button.type = 'button';
				button.value = 'Delete';
				button.className = 'imageDeleteNow';
				button.setAttribute( 'for', id );
				button.style.marginLeft = '1em';
				button.style.marginRight = '1em';
				
				var href = document.createElement( 'a' );
				href.href = '#';
				href.innerHTML = 'Cancel';
				href.className = 'imagePublishCancel';
				href.id = 'imagePublishCancel'+id;
				
				span.appendChild( button );
				span.appendChild( href );
				
				$('infoBumper'+id).appendChild( span );

				Behaviour.apply();

				new Effect.BlindDown( 'infoBumper'+id, {duration: .2} );
			}
		}
	},
	
	'.imageDeleteNow': function( element ) {
		element.onclick = function() {
			
			var id = element.getAttribute( 'for' );
			
			if( id ) {
	
				var spinner = document.createElement( 'img' );
				spinner.id = 'articleEditSpinner';
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				$('infoBumper'+id).appendChild( spinner );
				
				var url = BASEURL+'/'+MKEY+'/deleteImage';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id, evalScripts: true, method: 'post'}
				);
				
				window.deleteImage = function( data ) {

					if( data.saved ) {
					
						Element.remove( spinner );
						$('imagePublishCancel'+id).onclick();
											
						new Effect.Puff( 'imageContainer'+id );
						actionMessage( 'Image '+id+' deleted!' );
					}

				}
			}			
			

		}
	},
	
	'#crop':function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id ) {
				window.open( BASEURL+'/'+MKEY+'/cropImage/'+id, "Crop Image", "height=530,width=630, toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no");
			}
		}
	},
	
	'#rotateLeft': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id ) {
				var spinner = document.createElement( 'img' );
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				element.parentNode.parentNode.insertBefore(
					spinner,
					element.parentNode.nextSibling
				);

				var url = BASEURL+'/'+MKEY+'/rotateImage';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id+'&rotate=90', evalScripts: true, method: 'post'}
				);
				
				window.rotateImage = function( data ) {
					if( data.saved ) {
						Element.remove( spinner );
						
						var date = new Date();
						
						var thumb = $('imageThumbContainer'+id);
						var src = thumb.src;
						thumb.src = '#';
						thumb.src = src+'?'+date.getTime();;
					}
				}

			}
		}
	},

	'#rotateRight': function( element ) {
		element.onclick = function() {
			var id = element.getAttribute( 'for' );
			
			if( id ) {
				var spinner = document.createElement( 'img' );
				spinner.src = BASEURL.replace(
					new RegExp( 'index.php' ),
					'images/spinner.gif'
				);

				element.parentNode.parentNode.insertBefore(
					spinner,
					element.parentNode.nextSibling
				);

				var url = BASEURL+'/'+MKEY+'/rotateImage';
				
				new Ajax.Updater(
					'ajaxError',
					url,
					{parameters: 'id='+id+'&rotate=-90', evalScripts: true, method: 'post'}
				);
				
				window.rotateImage = function( data ) {
					if( data.saved ) {
						Element.remove( spinner );

						var date = new Date();
						
						var thumb = $('imageThumbContainer'+id);
						var src = thumb.src;
						thumb.src = '#';
						thumb.src = src+'?'+date.getTime();;
					}
				}

			}
		}
	}

};

Behaviour.register( imageBucketRules );